package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class day01 {
    public static void main(String[] args) {
        // 加载 MySQL 驱动类
        try {
            //Class.forName("com.mysql.cj.jbbc.Drivar")
            //类加载的时候，静态代码会执行
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("找不到 MySQL 驱动类: " + e.getMessage());
            return;
        }

        String url = "jdbc:mysql://localhost:3306/test1?useSSL=false";
        String username = "root";
        String password = "314159";

        try (Connection conn = DriverManager.getConnection(url, username, password);
             Statement statement = conn.createStatement()) {

            // 执行 SQL 语句
            String sql = "insert into dept(deptno, dname, loc) values(60, '人事部', '北京')";
            // statement.executeUpdate(sql) 专门执行增删改的语句
            // 返回值是影响数据库中的条数
            int count = statement.executeUpdate(sql);
            System.out.println("影响的行数: " + count);

        } catch (SQLException e) {
            System.out.println("数据库操作出错: " + e.getMessage());
        }
    }
}